<template>
  <el-dialog :close-on-click-modal="false" :visible.sync="dialog" :title="isAdd ? '新增打印导出模板' : '修改打印导出模板'" append-to-body width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item label="系统报表" prop="busMainData.reportCode">
        <el-select v-model="form.busMainData.reportCode" placeholder="请选系统报表">
          <el-option
            v-for="(item) in dicts.report_print_type"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="打印模板名称" prop="busMainData.reportName">
        <el-input v-model="form.busMainData.reportName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="排序" prop="busMainData.reportSort">
        <el-input-number v-model.number="form.busMainData.reportSort" :min="0" :max="999" controls-position="right" />
      </el-form-item>
      <el-form-item label="上传文件" prop="attachmentList">
        <FileUpload
          ref="fileUpload"
          :editable="form.busPageInfo.editable"
          :sup_this="sup_this"
          :file-limit="1"
          :is-direct-upload="true"
          accept=".xlsx" />
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','SYSPRINT_ALL','SYSPRINT_CHANGE','SYSPRINT_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','SYSPRINT_ALL','SYSPRINT_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toChange, toCopy } from '@/api/system/sysPrint'
import FileUpload from '@/views/components/FileUpload'
export default {
  components: { FileUpload },
  dictTypes: ['report_print_type'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    const validFile = (rule, value, callback) => {
      if (this.form.attachmentList && this.form.attachmentList.length === 0) {
        callback(new Error('请上传文件'))
      } else if (this.form.attachmentList && this.form.attachmentList.length !== 1) {
        callback(new Error('只上传1个文件'))
      } else {
        callback()
      }
    }
    return {
      isAdd: true,
      loading: false,
      dialog: false,
      form: {
        formSubmit: false,
        attachmentList: [],
        busPageInfo: {
          editable: true
        },
        busMainData: {
          id: null,
          reportName: '',
          reportCode: '',
          enableFlag: 'Y',
          flowType: '1',
          selfState: '0',
          reportSort: 0
        },
        busCommonState: {
        }
      },
      rules: {
        'busMainData.reportCode': [
          { required: true, message: '请选择系统报表', trigger: 'blur' }
        ],
        'busMainData.reportName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        'attachmentList': [
          { required: true, trigger: 'blur', validator: validFile }
        ]
      }
    }
  },
  watch: {
    'form.busMainData.reportCode'(value) {
      if (value && this.$refs.fileUpload) {
        this.dicts.report_print_type.forEach(dict => {
          if (dict.dictCode === value) {
            this.$refs.fileUpload.templatePath = dict.attribute1
            return
          }
        })
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
      this.form.attachmentList = []
    },
    viewMainData(mainId) {
      const data = { paramBean: { id: mainId }}
      viewMainData(data).then(res => {
        this.form = res.data
      })
      this.getOtherDetailData(mainId)
    },
    addData() {
      const data = { paramBean: { }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.id)
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    copyData(mainId) {
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getOtherDetailData(mainId)
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      toChange(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.oldMainId)
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    getOtherDetailData(mainId) {
      // 附档加载
      setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 200)
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.form.attachmentList = this.$refs.fileUpload.getFileList()
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
